package com.bgs.controller;

import com.bgs.config.JwtUtils;
import com.bgs.pojo.EmployeeVo;
import com.bgs.pojo.UserTokenVo;
import com.bgs.pojo.UserVo;
import com.bgs.service.LoginService;
import com.bgs.tool.R;
import com.sun.java.swing.plaf.windows.WindowsTextAreaUI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/user")
@CrossOrigin
public class LoginController {

    @Autowired
    private LoginService loginService;


    @PostMapping("/login")
    public R login(@RequestBody EmployeeVo employeeVo, HttpSession session){

        String phone=employeeVo.getPhone();
        String userpassword=employeeVo.getUserpassword();
        if (StringUtils.isEmpty(phone)){
                return R.error().message("请输入用户名");
        }
        if (StringUtils.isEmpty(userpassword)){
                return R.error().message("请输入密码");
        }
        UserTokenVo user=loginService.getLoginUser(phone,userpassword);
        if (user==null){
            return R.error().message("用户名或者密码错误");
        }else {
            session.setAttribute("user",user);
        }
        Integer eid = user.getEid();
        String jwtToken = JwtUtils.getJwtToken(user);
        user.setToken(jwtToken);

        return R.ok().data("user",user);
    }


        @PostMapping("/create")
        public R register(@RequestBody EmployeeVo employeeVo){
            boolean b=loginService.register(employeeVo);
            return R.ok().data("employeeVo",b);
        }

        @PostMapping("/update/{eid}")
        public Boolean password(@RequestBody  String userpassword, @PathVariable("eid") Integer eid){

        return loginService.password(userpassword,eid);
        }
}
